//
//  AlbumArtBLL.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/9.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "AlbumArtBLL.h"
#import "BMAArtist.h"
#import "CacheDataUtil.h"

#define kkCacheHotArtListKey @"kkCacheHotArtListKey"

@interface AlbumArtBLL () {

    NSArray *_sortKeyList;
    NSDictionary *_artList;
}

@end

@implementation AlbumArtBLL

@synthesize artSortKey = _sortKeyList;
@synthesize allArtList = _artList;

- (NSArray *)artSortKey{
    
    if (!_sortKeyList) {
        NSArray *arr=[CacheDataUtil unarchiveValueForKey:kkCacheHotArtListKey];
        if (arr&&[arr count]>0) {
            NSArray *keyList=[arr valueForKeyPath:@"firstchar"];
            _sortKeyList = [keyList sortedArrayUsingSelector:@selector(compare:)];
        }
    }
    return _sortKeyList;
}

- (NSDictionary *)allArtList{
    if (!_artList) {
         NSArray *arr=[CacheDataUtil unarchiveValueForKey:kkCacheHotArtListKey];
        if (arr&&[arr count]>0) {
            NSArray *keyList=[arr valueForKeyPath:@"firstchar"];
            
            NSArray *sortKeys = [keyList sortedArrayUsingSelector:@selector(compare:)];
            
            __block NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
            [sortKeys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
                
                NSPredicate *pre=[NSPredicate predicateWithFormat:@"SELF.firstchar == %@",key];
                NSArray *source = [arr filteredArrayUsingPredicate:pre];
                
                if (source&&[source count]>0) {
                    [dic setValue:source forKey:key];
                }
            }];
            
            _artList = dic;
            
        }
    }
    return _artList;
}

- (void)loadHotArt:(void (^) ())completion{

    BMAArtist *request = [[BMAArtist alloc] init];
    __weak BMAArtist *WSELF = request;
    [request setCZRequestURL:^NSString *{
        return [WSELF hotArtist:0 limit:60];
    }];
    
    [request starWithFinishedBlock:^(NSError *error, id result) {
        if (!error) {
            
            [self handlerArtData:result isCache:YES];
        }
        
        if (completion) {
            completion();
        }
    }];

}

- (void)searchArtWithArea:(NSInteger)area sex:(NSInteger)sex completion:(void (^) ())completion{
    BMAArtist *request = [[BMAArtist alloc] init];
    __weak BMAArtist *WSELF = request;
    [request setCZRequestURL:^NSString *{
        return [WSELF artistList:0 limit:60 area:area sex:sex order:1 abc:nil];
    }];
    
    [request starWithFinishedBlock:^(NSError *error, id result) {
        if (!error) {
            [self handlerArtData:result isCache:NO];
        }
        
        if (completion) {
            completion();
        }
    }];
}

- (void)handlerArtData:(NSDictionary *)result isCache:(BOOL)cache{
    
    NSArray *arr= [AlbumArt mj_objectArrayWithKeyValuesArray:result[@"artist"]];
    
    if (cache) {
        [CacheDataUtil setValueArchiver:arr forKey:kkCacheHotArtListKey];
    }
    if (arr&&[arr count]>0) {
        
        NSArray *keyList=[arr valueForKeyPath:@"firstchar"];
        NSSet *set = [NSSet setWithArray:keyList]; //去重复项
        _sortKeyList = [set.allObjects sortedArrayUsingSelector:@selector(compare:)];
        
        //NSLog(@"_sortKeyList =%@",_sortKeyList);

        __block NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
        [_sortKeyList enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSPredicate *pre=[NSPredicate predicateWithFormat:@"SELF.firstchar == %@",key];
            NSArray *source = [arr filteredArrayUsingPredicate:pre];
            
            if (source&&[source count]>0) {
                [dic setValue:source forKey:key];
            }
        }];
        
        _artList = dic;
        
    }
}

@end
